//
//  XYContentPageView.swift
//  XMGTV
//
//  Created by Gary on 2018/3/8.
//  Copyright © 2018年 coderwhy. All rights reserved.
//

import UIKit

private let kReuseIdentifier = "kReuseIdentifier"

protocol XYContentPageViewDelegate:class {
    
    func contenPageView(contenPageView:XYContentPageView,targetIndex:Int)
    
}

class XYContentPageView: UIView {
    
    weak var delegate:XYTitlePageViewDelegate?
    
    fileprivate var childVcs :[UIViewController]
    fileprivate var parentVc : UIViewController
    fileprivate var currentOffsetX :CGFloat = 0.0
    
    fileprivate lazy var collectionView :UICollectionView = {
        var flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: self.bounds.width, height: self.bounds.height)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = .horizontal
        
        var collection = UICollectionView(frame: CGRect(x: 0.0, y: 0.0, width: self.bounds.width, height: self.bounds.size.height), collectionViewLayout: flowLayout)
        
        collection.dataSource = self
        collection.delegate = self
        collection.showsHorizontalScrollIndicator = false
        collection.isPagingEnabled = true
        collection.scrollsToTop = false
        collection.backgroundColor = UIColor.clear
        collection.bounces = false
        
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kReuseIdentifier)
        
        return collection
    }()
    
    init(frame: CGRect , childVcs : [UIViewController] ,parentVC : UIViewController) {
        
        self.childVcs = childVcs
        self.parentVc = parentVC
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK:  ------ UI
extension XYContentPageView{
    
    fileprivate func setupUI() {
        
        for children in childVcs {
            parentVc.addChildViewController(children)
        }
        
        addSubview(collectionView)
        
    }
    
}

//MARK:  ------ 滑动试图的代理
extension XYContentPageView :UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVcs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kReuseIdentifier, for: indexPath)
        
        for view in cell.subviews {
            view.removeFromSuperview()
        }
        
        let vc = childVcs[indexPath.row]
        vc.view.frame = cell.contentView.bounds
        vc.view.backgroundColor = UIColor.randomColor()
        cell.addSubview(vc.view)
        
        return cell
    }
    
}
//MARK:  ------ 滑动代理
extension XYContentPageView:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollViewEndTarget()
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        if !scrollView.isDecelerating{
            scrollViewEndTarget()
        }
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        currentOffsetX = scrollView.contentOffset.x
    }
    
    private func scrollViewEndTarget(){
        
        var targetIndex : Int = 0
        
        if currentOffsetX < collectionView.contentOffset.x{//左滑
            
           targetIndex = Int((collectionView.contentOffset.x - currentOffsetX)/bounds.width)
            
        }else{ //右滑
            
            targetIndex = Int((currentOffsetX - collectionView.contentOffset.x)/bounds.width)
        }
        
        print(targetIndex)
    }
    
}

//MARK:  ------ 外部代理
extension XYContentPageView:XYTitlePageViewDelegate {
    
    func titlePageView(titlePage: XYTitlePageView, targetIndex: Int) {
        collectionView.setContentOffset(CGPoint(x: CGFloat(targetIndex)*bounds.width, y: 0), animated: false)
    }
    
}

